package net.sf.nlp.ltag.data;

import java.util.HashMap;
import java.util.Map;

public class LTAGStatistics 
{
	private static long beginningTreeCount = 0;
	
	private static Map<ArrayKey, Double> statistics = new HashMap<ArrayKey, Double>();
	
	public static void setBeginningTreeCount(long beginningTreeCount) {
		LTAGStatistics.beginningTreeCount = beginningTreeCount;
	}

	public static void addStatistic(Long srcSuperTreeId, Long destSuperTreeId, Integer srcNodeLocation, String operator, Long frequence, Long total)
	{
		ArrayKey key = null;
		double freq = 0;
		
		if (srcNodeLocation == -1)
		{
			key = new ArrayKey(srcSuperTreeId);
			
			freq = (double)frequence / beginningTreeCount;
		}
		else
		{
			if (destSuperTreeId != null)
				key = new ArrayKey(srcSuperTreeId, destSuperTreeId, srcNodeLocation, operator);
			else
				key = new ArrayKey(srcSuperTreeId, srcNodeLocation, operator);
			
			freq = (double)frequence / total;
		}
		
		statistics.put(key, freq);
	}
	
	public static double getStatistics(ArrayKey key)
	{ 
		Double value = statistics.get(key);
		
		if (value == null)
			value = 0.0001;
		
		return -Math.log(value);
	}
	
	public static ArrayKey buildKey4Operator(Operator operator)
	{
		ArrayKey key = new ArrayKey(operator.getTree1().getId(), operator.getTree2().getId(), operator.getTree1Node().getLocation(), operator.getOperator());
		
		return key;
	}
	
	public static ArrayKey buildKey4BeginingTree(LTAGTree tree)
	{
		ArrayKey key = new ArrayKey(tree.getId());
		
		return key;
	}
	
	public static ArrayKey buildKey4NoAdjunction(LTAGTree tree, LTAGNode node)
	{
		ArrayKey key = new ArrayKey(tree.getId(), node.getLocation(), "*");
		
		return key;
	}
}
